﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;

namespace GauntletFramework.StateManagment.Transition
{
    public class TransitionState : State
    {
        public TransitionState(float Time)
        {
            this.Time = Time;   
        }
        public override bool StartTransition { get { return false; } }
        public override bool EndTransition { get { return false; } }
        protected State Start;
        protected State End;
        protected float Progress = 0f;
        protected float Time;
        private bool needTrans = true;
        private bool TargetsInitialised = false;
        protected RenderTarget2D StartRenderTexture2D, EndRenderTexture2D;

        public void AssignStates(State Start, State End)
        {
            if (this.Start == null && this.End == null)
            {
                this.Start = Start;
                this.End = End;
            }
            needTrans = (End.GameOrientation != Start.GameOrientation);
        }

        public override void Update(GameTime gameTime)
        {
            if (Progress >= 0.5f && needTrans)
            {
                GameOrientation GO = End.GameOrientation;

                if (GO == GameOrientation.Horizontal)
                {
                    GraphicsDeviceManager graphics = StaticGame.Game.graphics;
                    graphics.PreferredBackBufferWidth = 800;
                    graphics.PreferredBackBufferHeight = 480;
                    graphics.SupportedOrientations = DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight;
                    graphics.ApplyChanges();
                }
                else if (GO == GameOrientation.Vertical)
                {
                    GraphicsDeviceManager graphics = StaticGame.Game.graphics;
                    graphics.PreferredBackBufferWidth = 480;
                    graphics.PreferredBackBufferHeight = 800;
                    graphics.SupportedOrientations = DisplayOrientation.Portrait;
                    graphics.ApplyChanges();
                }
                else if (GO == GameOrientation.Both)
                {
                    GraphicsDeviceManager graphics = StaticGame.Game.graphics;
                    graphics.SupportedOrientations = DisplayOrientation.Default | DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight | DisplayOrientation.Portrait;
                    graphics.ApplyChanges();
                }
            }

            Progress += (float)gameTime.ElapsedGameTime.TotalSeconds / Time;
            if (Progress > 1.0f)
                Finished = true;

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            if (!TargetsInitialised)
                if (this.Start != null && this.End != null)
                {
                    SpriteBatch Oldsb = StaticGame.Game.SpriteBatch;


                    StartRenderTexture2D = new RenderTarget2D(StaticGame.GraphicsDevice, StaticGame.ScreenSize.Width, StaticGame.ScreenSize.Height);
                    RenderTargetManager.SetRenderTarget(StartRenderTexture2D);
                    StaticGame.Game.SpriteBatch = new SpriteBatch(StaticGame.GraphicsDevice);
                    StaticGame.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied);
                    Start.Draw(new GameTime());
                    StaticGame.SpriteBatch.End();
                    RenderTargetManager.UnSetRenderTarget();

                    EndRenderTexture2D = new RenderTarget2D(StaticGame.GraphicsDevice, StaticGame.ScreenSize.Width, StaticGame.ScreenSize.Height);
                    RenderTargetManager.SetRenderTarget(EndRenderTexture2D);
                    StaticGame.Game.SpriteBatch = new SpriteBatch(StaticGame.GraphicsDevice);
                    StaticGame.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied);
                    End.Draw(new GameTime());
                    StaticGame.SpriteBatch.End();
                    RenderTargetManager.UnSetRenderTarget();


                    StaticGame.Game.SpriteBatch = Oldsb;
                
                    TargetsInitialised = true;
                }
        }
    }
}
